package com.nanhai.buddhist.college.module.collegebase.controller.teacher.studentroll;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.datapermission.core.annotation.DataPermission;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.vo.ClassesStudentListPageVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentroll.vo.StudentRollPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentroll.vo.StudentRollRespVO;
import com.nanhai.buddhist.college.module.collegebase.convert.studentroll.StudentRollConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.professions.ProfessionsDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentroll.StudentRollDO;
import com.nanhai.buddhist.college.module.collegebase.service.professions.ProfessionsService;
import com.nanhai.buddhist.college.module.collegebase.service.studentroll.StudentRollService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.*;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

@Api(tags = "教师端 - 学员信息")
@RestController
@RequestMapping("/collegebase/student-roll")
@Validated
public class TeacherStudentRollController {


    @Resource
    private StudentRollService studentRollService;

    @Resource
    private ProfessionsService professionsService;

    @GetMapping("/page")
    @ApiOperation("获得学员信息分页")
//    @PreAuthorize("@ss.hasPermission('collegebase:student-roll:query')")
    public CommonResult<PageResult<StudentRollRespVO>> getStudentRollPage(@Valid StudentRollPageReqVO pageVO) {
        PageResult<StudentRollDO> pageResult = studentRollService.getStudentRollPage(pageVO);
        return success(StudentRollConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/page2")
    @ApiOperation("获得学员信息分页")
    public CommonResult<PageResult<StudentRollRespVO>> getStudentRollPage2(@Valid ClassesStudentListPageVO pageVO) {
        PageResult<StudentRollDO> pageResult = studentRollService.getStudentRespDTOByClassesId(pageVO);
        PageResult<StudentRollRespVO> resultPage = StudentRollConvert.INSTANCE.convertPage(pageResult);
        Set<String> ids = CollectionUtils.convertSet(resultPage.getList(), StudentRollRespVO::getProfessionsId);
        List<ProfessionsDO> objectDataList = professionsService.getProfessionsList(ids);
        if (CollectionUtils.isNotEmpty(objectDataList)) {
            Map<String, ProfessionsDO> objectIdMap = CollectionUtils.convertMap(objectDataList, ProfessionsDO::getId);
            resultPage.getList().stream().forEach(s -> {
                s.setProfessionName(Optional.ofNullable(objectIdMap).map(v -> v.get(s.getProfessionsId())).map(ProfessionsDO::getProfessionName).orElse(""));
            });
        }
        return success(resultPage);
    }


}